<?php

$info = $this->prototype->info();
$tablename = $info[WSB_Db_Table::NAME];
$primary = reset($info[WSB_Db_Table::PRIMARY]);

// Controller Uebersetzungsfile
echo sprintf('<%s>%s</%s>', $this->titleType, $this->translate($tablename . '.list.title'), $this->titleType);

// ---------------------------------------------------------------------------------------
// Einfache Filterbox darstellen
$options = array();
$options[''] = '';
foreach($this->prototype->getListCols() AS $col) {
	// Model Uebersetzungsfile
    $options[$col] = $this->prototype->translate($col);
}
$key = key_exists('key', $this->filter) ? $this->filter['key'] : '';
$value = key_exists('value', $this->filter) ? $this->filter['value'] : '';

$filtering = '<form action="' . $this->controller->getDefaultListUrl() . '" method="post">';
$filtering .= $this->formHidden('offset', 0);
$filtering .= $this->formHidden('filter[model]', $this->modelIndex);
$filtering .= $this->formSelect('filter[key]', $key, null, $options);
$filtering .= $this->formText('filter[value]', $value);
$filtering .= $this->formSubmit('filter[set]', 'Set');
if(!empty($key) && !empty($value)) {
    $filtering .= $this->formSubmit('filter[reset]', 'Reset');
}
$filtering .= '</form>';
echo sprintf('<div style="float:right;text-align:right;padding-bottom:1px;">%s</div>', $filtering);

if(($url = $this->controller->getDefaultAddUrl($this->modelIndex)) != '') {
	$attributes = array('class' => 'add-link');
	// Controller Uebersetzungsfile
	echo sprintf('<div>%s</div>', WSB_Controller_Url::getHtmlLink($this->translate($tablename . '.list.add'), $url, $attributes));
}

// ---------------------------------------------------------------------------------------
$table = new WSB_Table('class="style-1" style="clear:both;"');
$table->setAutoGrow(true);
$table->setAutoFill('&nbsp;');

// THs, Colgroup
$orderColumnIndex = -1;
$orderDirection = 'asc';
$columns = array();
$colgroup = array();
foreach($this->prototype->getListCols() AS $i=>$col) {

	// Model Uebersetzungsfile
	$translation = $this->prototype->translate($col);

	// Sortierung
	array_push($columns, WSB_Controller_Url::getHtmlLink($translation, $this->controller->getDefaultListUrl(null, $col, $this->modelIndex)));

	if($primary == $col) {
		array_push($colgroup, 'width="5%"');
	} else {
		array_push($colgroup, '');
	}

	if($col == $this->orderColumn) {
		if($this->orderDirection == 'DESC') {
			$orderDirection = 'desc';
		}
		$orderColumnIndex = $i;
	}

}
array_push($columns, '&nbsp;');
array_push($colgroup, 'width="5%"');

$table->setColGroup($colgroup);
$table->addRow($columns, null, 'th');
if($orderColumnIndex >= 0) {
	$table->setCellAttributes(0, $orderColumnIndex, array('class' => 'sort sort-' . $orderDirection));
}

$class = 'even';
$rowIndex = 0;
foreach($this->collection AS $model) {
	$colIndex = 0;
	// TDs
	$columns = array();
	foreach($this->prototype->getListCols() AS $col) {
		if(in_array($col, array('created', 'modified'))) {
			$dateTime = '';
			if($model->$col > 0) {
				$dateTime = date('Y.m.d H:i:s', $model->$col);
			}
			array_push($columns, $dateTime);
		}
		else {
			array_push($columns, $model->$col);
		}
		$colIndex++;
	}
	$links = implode('', $this->controller->buildListItemHtmlLinks($model, $this->modelIndex));
	array_push($columns, $links);
	$table->addRow($columns, array('class' => $class), 'td', true);
	$rowIndex++;
	// Icons-Spalte
	$table->setCellAttributes($rowIndex, $colIndex, array('style' => 'white-space: nowrap;'));
	$class = ($class == 'odd') ? 'even' : 'odd';
}

// Einfache Paginierung
$paging = '';
if($this->offset > 0) {
	$paging .= WSB_Controller_Url::getHtmlLink($this->htmlImage('../WSB/design/default/skin/default/img/paging-left-on.gif'), $this->controller->getDefaultListUrl(max(0, $this->offset-$this->count), null, $this->modelIndex));
} else {
	$paging .= $this->htmlImage('../WSB/design/default/skin/default/img/paging-left-off.gif');
}
if(($this->offset + $this->count) < $this->total) {
	$paging .= WSB_Controller_Url::getHtmlLink($this->htmlImage('../WSB/design/default/skin/default/img/paging-right-on.gif'), $this->controller->getDefaultListUrl($this->offset+$this->count, null, $this->modelIndex));
} else {
	$paging .= $this->htmlImage('../WSB/design/default/skin/default/img/paging-right-off.gif');
}

// Foot
$i = 0;
$columns = array();
foreach($this->prototype->getListCols() AS $col) {
    if($i == 0) {
        array_push($columns, sprintf($this->translate('pagingStdMessage'), $this->total, $this->offset+1, min($this->total, $this->offset+$this->count+1)));
    } else {
        array_push($columns, '');
    }
    $i++;
}
array_push($columns, $paging);
$table->addRow($columns, array('class' => 'foot'), 'td', true);
$table->setCellAttributes($rowIndex+1, 0, array('colspan' => count($this->prototype->getListCols())));


/* Die Kopf- und Fusszeile mit beruecksichtigen (deshalb 2) */
if($table->getRowCount() > 2) {
	echo $table->render();
} else {
	// Controller Uebersetzungsfile
	echo sprintf('<p>%s</p>', $this->translate('noEntriesInDb'));
}

?>